/*+**************************************************************************/
/***                                                                      ***/
/***   This file is distributed under a BSD license.                      ***/
/***   See LICENSE.txt for details.                                       ***/
/***                                                                      ***/
/**************************************************************************+*/

material tSharpen4Material
{
  vs
  {
    asc vs_3_0                 // hlsl code
    {      
      cbuffer tSharpen4MaterialVSPara : register(c0) : slot vs 0
      { 
        row_major float4x4 mvp;     
      };

      use tSharpen4MaterialVSPara;
      
      void main
      (
        in float3 in_pos : POSITION,
        in float4 in_color : COLOR0,
        in float2 in_uv0 : TEXCOORD0,
        out float2 out_uv0 : TEXCOORD0,
        out float4 out_pos : POSITION,
      )
      {
        out_uv0 = in_uv0;
        out_pos = mul(float4(in_pos,1),mvp);
      }      
    }
  }

  ps
  {
    asc ps_3_0
    {
      cbuffer tSharpen4MaterialPSPara : register(c0) : slot ps 0
      { 
        float rw;
        float rh;
        float factors[16];
      };
      
      use tSharpen4MaterialPSPara;

      sampler2D s0 : register(s0);  
          
      void main
      (
        in float2 in_uv : TEXCOORD0,        
        out float4 result : COLOR0
      )
      {      
        int x;
        int y;     
        result = float4(0,0,0,0);

        for (y=0;y<4;y++)
        {
          for (x=0;x<4;x++)
          {
            float2 uv;
            uv.x = in_uv.x + (x-1) * rw;
            uv.y = in_uv.y + (y-1) * rh;
            result = result + tex2D(s0,uv) * factors[y*4+x];
          }
        }
      }
    }
  }
};




material tLTSMaterial
{
  vs
  {
    asc vs_3_0                 // hlsl code
    {      
      cbuffer tLTSMaterialVSPara : register(c0) : slot vs 0
      { 
        row_major float4x4 mvp;     
      };

      use tLTSMaterialVSPara;
      
      void main
      (
        in float3 in_pos : POSITION,
        in float4 in_color : COLOR0,
        in float2 in_uv0 : TEXCOORD0,
        out float2 out_uv0 : TEXCOORD0,
        out float4 out_pos : POSITION,
      )
      {
        out_uv0 = in_uv0;
        out_pos = mul(float4(in_pos,1),mvp);
      }      
    }
  }

  ps
  {
    asc ps_3_0
    {
      cbuffer tLTSMaterialPSPara : register(c0) : slot ps 0
      { 
        float scale;
      };
      
      use tLTSMaterialPSPara;

      sampler2D s0 : register(s0);  
          
      void main
      (
        in float2 in_uv : TEXCOORD0,        
        out float4 result : COLOR0
      )
      { 
        float4 c;
        c = tex2D(s0,in_uv);
        result.x = exp2(c.x*scale);
        result.y = exp2(c.y*scale);
        result.z = exp2(c.z*scale);
        result.w = c.w;
      }
    }
  }
};



material tSTLMaterial
{
  vs
  {
    asc vs_3_0                 // hlsl code
    {      
      cbuffer tSTLMaterialVSPara : register(c0) : slot vs 0
      { 
        row_major float4x4 mvp;     
      };

      use tSTLMaterialVSPara;
      
      void main
      (
        in float3 in_pos : POSITION,
        in float4 in_color : COLOR0,        
        in float2 in_uv0 : TEXCOORD0,
        out float2 out_uv0 : TEXCOORD0,
        out float4 out_pos : POSITION,
      )
      {
        out_uv0 = in_uv0;
        out_pos = mul(float4(in_pos,1),mvp);
      }      
    }
  }

  ps
  {
    asc ps_3_0
    {
      cbuffer tSTLMaterialPSPara : register(c0) : slot ps 0
      { 
        float ivexpo;
      };
      
      use tSTLMaterialPSPara;

      sampler2D s0 : register(s0);  
          
      void main
      (
        in float2 in_uv : TEXCOORD0,        
        out float4 result : COLOR0
      )
      { 
        float4 c;
        c = tex2D(s0,in_uv);
        result.x = log2(c.x)*ivexpo;
        result.y = log2(c.y)*ivexpo;
        result.z = log2(c.z)*ivexpo;
        result.w = c.w;        
      }
    }
  }
};



material tBloomMaskMat
{  
  vs
  {
    asc vs_3_0
    {

      cbuffer tBloomMaskVSPara : register(c0) : slot vs 0
      { 
        row_major float4x4 mvp;     
      };

      use tBloomMaskVSPara;

      void main
      (
        in float3 in_pos : POSITION, 
        in float4 in_color : COLOR0,
        in float2 in_uv0 : TEXCOORD0,
        
        out float2 out_uv0 : TEXCOORD0,
        out float4 out_pos : POSITION,
      )
      {
        out_uv0 = in_uv0;
        out_pos = mul(float4(in_pos,1),mvp);
      }      
    }
  }

  ps
  {
    asc ps_3_0
    {
      cbuffer tBloomMaskPSPara : register(c0) : slot ps 0
      { 
        float4 HighlightCol;
        float4 MaskCol;
        float4 MaskScaleBias;
        float4 ZBufScaleBias;
        float4 AlphaScaleBias;
        float4 GrayCol;
        float4 BlendFactor;
        float4 Misc;          // PreBlurFilterSize.xy | 
      };

      permute tBloomMaskPSPerm
      {
        GRAY;
        MSC;    //Mulitply Screen Color
        ZBUF;
        ABUF;        
        TAP { TAP0,TAP5,TAP9 };
      };
      
      use tBloomMaskPSPara;
      use tBloomMaskPSPerm;

      sampler2D s0 : register(s0);
      sampler2D s1 : register(s1) : pif(ZBUF);

      float scaleandclamp(float v, float scale, float bias)
      {        
        v = clamp(v+bias,0.0,1.0) * scale;
        return v;
      }

      void main
      (
        in float2 in_uv : TEXCOORD0,        
        out float4 result : COLOR0
      )
      {         
        float4 c;
        float4 d;

        c = tex2D(s0,in_uv);        

        pif(TAP==TAP5)
        {
          c += tex2D(s0,in_uv+float2(Misc.x* 1,Misc.y* 0));
          c += tex2D(s0,in_uv+float2(Misc.x*-1,Misc.y* 0));
          c += tex2D(s0,in_uv+float2(Misc.x* 0,Misc.y* 1));
          c += tex2D(s0,in_uv+float2(Misc.x* 0,Misc.y*-1));
          c *= 1.0/5;
        }
        pif(TAP==TAP9)
        {
          c += tex2D(s0,in_uv+float2(Misc.x* 0,Misc.y*-1));
          c += tex2D(s0,in_uv+float2(Misc.x* 1,Misc.y*-1));
          c += tex2D(s0,in_uv+float2(Misc.x*-1,Misc.y* 0));
          c += tex2D(s0,in_uv+float2(Misc.x* 0,Misc.y* 0));
          c += tex2D(s0,in_uv+float2(Misc.x* 1,Misc.y* 0));
          c += tex2D(s0,in_uv+float2(Misc.x*-1,Misc.y* 1));
          c += tex2D(s0,in_uv+float2(Misc.x* 0,Misc.y* 1));
          c += tex2D(s0,in_uv+float2(Misc.x* 1,Misc.y* 1));
          c *= 1.0/9;
        }

        float gray = 0.0f;

        pif(GRAY)
        { 
          float4 t;
          t.x = (c.x - HighlightCol.x) * MaskCol.x;
          t.y = (c.y - HighlightCol.y) * MaskCol.y;
          t.z = (c.z - HighlightCol.z) * MaskCol.z;
          gray  = scaleandclamp(t.x + t.y + t.z, MaskScaleBias.x, MaskScaleBias.y);
        }

        pif(ZBUF)
        {
          float depth = tex2D(s1,in_uv);          
          gray   = gray  - (depth * ZBufScaleBias.x) + ZBufScaleBias.y;
        }

        pif(ABUF)
        {          
          float f = scaleandclamp(c.w, AlphaScaleBias.x, AlphaScaleBias.y);
          gray   = gray + f;
        }        


        pif(MSC)
        {
          d.r = scaleandclamp(gray, GrayCol.x, GrayCol.w);
          d.g = scaleandclamp(gray, GrayCol.y, GrayCol.w);
          d.b = scaleandclamp(gray, GrayCol.z, GrayCol.w);          

          d.r = lerp(d.r, c.r*d.r, BlendFactor.r);
          d.g = lerp(d.g, c.g*d.g, BlendFactor.g);
          d.b = lerp(d.b, c.b*d.b, BlendFactor.b);
        }
        pelse
        {
          d.r = gray * GrayCol.x + GrayCol.w;
          d.g = gray * GrayCol.y + GrayCol.w;
          d.b = gray * GrayCol.z + GrayCol.w;          
        }

        result.r = d.r;
        result.g = d.g;
        result.b = d.b;
        result.w = c.w;  
      }
    }
  }


  header
  {
    enum ExtraBits
    {
      EXTRA_GRAY    = 0x02,
      EXTRA_MSC     = 0x04,
      EXTRA_ZBUF    = 0x08,
      EXTRA_ABUF    = 0x10,
      EXTRA_PRETAP0 = 0x00,
      EXTRA_PRETAP5 = 0x40,
      EXTRA_PRETAP9 = 0x80,
    };
    sInt Extra;
  }
  new
  {
    Extra = 0;
  }

  prepare
  {
    sInt ps=0;
    
    if (Extra&EXTRA_ABUF)
    {
      ps |= tBloomMaskPSPermMask_ABUF;
    }

    if (Extra&EXTRA_ZBUF)
    {
      ps |= tBloomMaskPSPermMask_ZBUF;
    }

    if (Extra&EXTRA_MSC)
    {
      ps |= tBloomMaskPSPermMask_MSC;
    }

    if (Extra&EXTRA_GRAY)
    {
      ps |= tBloomMaskPSPermMask_GRAY;
    }

    if(Extra & EXTRA_PRETAP5)
      ps |= tBloomMaskPSPerm_TAP5;
    if(Extra & EXTRA_PRETAP9)
      ps |= tBloomMaskPSPerm_TAP9;


    VertexShader = VS();    
    PixelShader = PS(ps);        
  }

};







material tBloomCompMat
{  
  vs
  {
    asc vs_3_0
    {

      cbuffer tBloomCompVSPara : register(c0) : slot vs 0
      { 
        row_major float4x4 mvp;     
      };

      use tBloomCompVSPara;

      void main
      (
        in float3 in_pos : POSITION,
        in float4 in_color : COLOR0,
        in float2 in_uv0 : TEXCOORD0,
        out float2 out_uv0 : TEXCOORD0,
        out float4 out_pos : POSITION,
      )
      {
        out_uv0 = in_uv0;
        out_pos = mul(float4(in_pos,1),mvp);
      }      
    }
  }

  ps
  {
    asc ps_3_0
    {
      cbuffer tBloomCompPSPara : register(c0) : slot ps 0
      { 
        float4 ScreenCol;
        float4 BloomCol;               
      };

      permute tBloomCompPSPerm
      {
        Blend{Add,Screen,Mul};
      };
            
      use tBloomCompPSPara;
      use tBloomCompPSPerm;

      sampler2D s0 : register(s0);
      sampler2D s1 : register(s1);
          
      void main
      (
        in float2 in_uv : TEXCOORD0,        
        out float4 result : COLOR0
      )
      {         
        float4 a = result = tex2D(s0,in_uv);
        float4 b = result = tex2D(s1,in_uv);
        
        float ar = a.x * ScreenCol.x;
        float ag = a.y * ScreenCol.y;
        float ab = a.z * ScreenCol.z;
          
        float br = b.x * BloomCol.x;
        float bg = b.y * BloomCol.y;
        float bb = b.z * BloomCol.z;
        
        pif(Blend==Screen)
        {           
          result.r = ar + br - ar * br;
          result.g = ag + bg - ag * bg;
          result.b = ab + bb - ab * bb;
        }
        pif(Blend==Add)
        {        
          result.r = a.x * ScreenCol.x + b.x * BloomCol.x;
          result.g = a.y * ScreenCol.y + b.y * BloomCol.y;
          result.b = a.z * ScreenCol.z + b.z * BloomCol.z;
        }
        pif(Blend==Mul)
        {        
          result.r = a.x * ScreenCol.x * b.x;
          result.g = a.y * ScreenCol.y * b.y;
          result.b = a.z * ScreenCol.z * b.z;
        }
        result.w = a.w;  
      }
    }
  }
  
  header
  {
    enum ExtraBits
    {
      EXTRA_ADD  = 0,
      EXTRA_SCREEN   = 1,
      EXTRA_MUL   = 2,
    };
    sInt Extra;
  }
  new
  {
    Extra = 0;
  }

  prepare
  {
    sInt ps=0;
    
    switch(Extra & 3)
    {
    case EXTRA_ADD:
      ps |= tBloomCompPSPerm_Add;
      break;
    case EXTRA_SCREEN:
      ps |= tBloomCompPSPerm_Screen;
      break;
    case EXTRA_MUL:
      ps |= tBloomCompPSPerm_Mul;
      break;
    }

    VertexShader = VS();    
    PixelShader = PS(ps);        
  }  
};




material tPromistCompMat
{  
  vs
  {
    asc vs_3_0
    {

      cbuffer tPromistCompVSPara : register(c0) : slot vs 0
      { 
        row_major float4x4 mvp;     
      };

      use tPromistCompVSPara;

      void main
      (
        in float3 in_pos : POSITION,
        in float4 in_color : COLOR0,
        in float2 in_uv0 : TEXCOORD0,
        out float2 out_uv0 : TEXCOORD0,
        out float4 out_pos : POSITION,
      )
      {
        out_uv0 = in_uv0;
        out_pos = mul(float4(in_pos,1),mvp);
      }      
    }
  }

  ps
  {
    asc ps_3_0
    {
      cbuffer tPromistCompPSPara : register(c0) : slot ps 0
      { 
        float4 ScreenNormalCol;
        float4 ScreenBlurredCol;               
      };

      
      use tPromistCompPSPara;

      sampler2D s0 : register(s0);
      sampler2D s1 : register(s1);
      sampler2D s2 : register(s2);
          
      void main
      (
        in float2 in_uv : TEXCOORD0,        
        out float4 result : COLOR0
      )
      {         
        float4 a = tex2D(s0,in_uv);  //Screen
        float4 b = tex2D(s1,in_uv);  //Blurred Screen
        float4 c = tex2D(s2,in_uv);  //Mask

        a.x = a.x * ScreenNormalCol.x;
        a.y = a.y * ScreenNormalCol.y;
        a.z = a.z * ScreenNormalCol.z;

        b.x = b.x * ScreenBlurredCol.x;
        b.y = b.y * ScreenBlurredCol.y;
        b.z = b.z * ScreenBlurredCol.z;
        
        result.r = lerp(a.x, b.x, c.x);
        result.g = lerp(a.y, b.y, c.y);
        result.b = lerp(a.z, b.z, c.z);
        result.w = a.w;  
      }
    }
  }
};


material tCopyMat
{  
  vs
  {
    asc vs_3_0
    {

      cbuffer tCopyVSPara : register(c0) : slot vs 0
      { 
        row_major float4x4 mvp;     
      };

      use tCopyVSPara;

      void main
      (
        in float3 in_pos : POSITION,
        in float2 in_uv0 : TEXCOORD0,
        out float2 out_uv0 : TEXCOORD0,
        out float4 out_pos : POSITION,
      )
      {
        out_uv0 = in_uv0;
        out_pos = mul(float4(in_pos,1),mvp);
      }      
    }
  }

  ps
  {
    asc ps_3_0
    {
      cbuffer tCopyPSPara : register(c0) : slot ps 0
      { 
        float unused;
      };

      
      use tCopyPSPara;

      sampler2D s0 : register(s0);
          
      void main
      (
        in float2 in_uv : TEXCOORD0,        
        out float4 result : COLOR0
      )
      {         
        result = tex2D(s0,in_uv);
      }
    }
  }
};







material tDoF3MaskMat
{  
  vs
  {
    asc vs_3_0
    {

      cbuffer tDoF3MaskVSPara : register(c0) : slot vs 0
      { 
        row_major float4x4 mvp;     
      };

      use tDoF3MaskVSPara;

      void main
      (
        in float3 in_pos : POSITION,
        in float4 in_color : COLOR0,
        in float2 in_uv0 : TEXCOORD0,
        out float2 out_uv0 : TEXCOORD0,
        out float4 out_pos : POSITION,
      )
      {
        out_uv0 = in_uv0;
        out_pos = mul(float4(in_pos,1),mvp);
      }      
    }
  }

  ps
  {
    asc ps_3_0
    {
      cbuffer tDoF3MaskPSPara : register(c0) : slot ps 0
      { 
        float4 Params;        
      };

      
      use tDoF3MaskPSPara;

      sampler2D s0 : register(s0);  //
          
      void main
      (
        in float2 in_uv   : TEXCOORD0,        
        out float4 result : COLOR0
      )
      {         
        float a = tex2D(s0,in_uv);
                        
        if (a<Params.x)
        {
          a = (Params.z - a) * Params.w;                
        }
        else if (a>Params.z)
        {
          a = (a - Params.z) * Params.w;      
        }
        else
        {
          a = 0.0;
        }
                
        //a = a * Params.x;
        a = clamp (a,0.0,1.0);
        result.r = a;
        result.g = a;
        result.b = a;
        result.a = a;        
      }
    }
  }
};



material tDoF3CompMat
{  
  vs
  {
    asc vs_3_0
    {

      cbuffer tDoF3CompVSPara : register(c0) : slot vs 0
      { 
        row_major float4x4 mvp;     
      };

      use tDoF3CompVSPara;

      void main
      (
        in float3 in_pos : POSITION,
        in float4 in_color : COLOR0,
        in float2 in_uv0 : TEXCOORD0,
        out float2 out_uv0 : TEXCOORD0,
        out float4 out_pos : POSITION,
      )
      {
        out_uv0 = in_uv0;
        out_pos = mul(float4(in_pos,1),mvp);
      }      
    }
  }

  ps
  {
    asc ps_3_0
    {
      cbuffer tDoF3CompPSPara : register(c0) : slot ps 0
      { 
        float4 ScreenNormalCol;
        float4 ScreenBlurredCol;               
      };

      
      use tDoF3CompPSPara;

      sampler2D s0 : register(s0);
      sampler2D s1 : register(s1);
      sampler2D s2 : register(s2);
          
      void main
      (
        in float2 in_uv : TEXCOORD0,        
        out float4 result : COLOR0
      )
      {         
        float4 a = tex2D(s0,in_uv);  //Screen
        float4 b = tex2D(s1,in_uv);  //Blurred Screen
        float4 c = tex2D(s2,in_uv);  //Mask

        a.x = a.x * ScreenNormalCol.x;
        a.y = a.y * ScreenNormalCol.y;
        a.z = a.z * ScreenNormalCol.z;

        b.x = b.x * ScreenBlurredCol.x;
        b.y = b.y * ScreenBlurredCol.y;
        b.z = b.z * ScreenBlurredCol.z;
        
        result.r = lerp(a.x, b.x, c.r);
        result.g = lerp(a.y, b.y, c.r);
        result.b = lerp(a.z, b.z, c.r);
        result.w = a.w;  
      }
    }
  }
};




material tGlowGradientMat
{
  vs
  {
    asc vs_3_0                 // hlsl code
    {      
      cbuffer tGlowGradientMatVSPara : register(c0) : slot vs 0
      { 
        row_major float4x4 mvp;     
      };

      use tGlowGradientMatVSPara;
      
      void main
      (
        in float3 in_pos : POSITION,
        in float4 in_color : COLOR0,
        in float2 in_uv0 : TEXCOORD0,
        out float2 out_uv0 : TEXCOORD0,
        out float4 out_pos : POSITION,
      )
      {
        out_uv0 = in_uv0;
        out_pos = mul(float4(in_pos,1),mvp);
      }      
    }
  }

  ps
  {
    asc ps_3_0
    {
      cbuffer tGlowGradientMatPSPara : register(c0) : slot ps 0
      { 
        float4 in_color;
        float4 out_color;
        float4 factors;
      };
      
      use tGlowGradientMatPSPara;

      
          
      void main
      (
        in float2 uv : TEXCOORD0,        
        out float4 result : COLOR0
      )
      {      
        float vv = uv.x;
		    float vx=vv*factors.x;
		    if (vx>1.0f) vx=1.0f;
		    float vy=1.0f-vx;    

        float r=in_color.x*vx + out_color.x*vy;
        float g=in_color.y*vx + out_color.y*vy;
        float b=in_color.z*vx + out_color.z*vy;

        float tr=factors.z-exp(-vv*factors.y);

        r=r*tr;
        g=g*tr;
        b=b*tr;

        result = float4(r,g,b,1.0f);
      }
    }
  }
};

material tGlowMaskMat
{
  vs
  {
    asc vs_3_0                 // hlsl code
    {      
      cbuffer tGlowMaskMatVSPara : register(c0) : slot vs 0
      { 
        row_major float4x4 mvp;     
      };

      use tGlowMaskMatVSPara;
      
      void main
      (
        in float3 in_pos : POSITION,
        in float4 in_color : COLOR0,
        in float2 in_uv0 : TEXCOORD0,
        out float2 out_uv0 : TEXCOORD0,
        out float4 out_pos : POSITION,
      )
      {
        out_uv0 = in_uv0;
        out_pos = mul(float4(in_pos,1),mvp);
      }      
    }
  }

  ps
  {
    asc ps_3_0
    {
      cbuffer tGlowMaskMatPSPara : register(c0) : slot ps 0
      { 
        float4 textpar; //.x= 1/width, .y= 1/height
        float4 maskpar;
      };
      
      permute tGlowMaskMatPSPerm
      {
        EDGE;      
      };

      use tGlowMaskMatPSPara;
      use tGlowMaskMatPSPerm;

      sampler2D s0 : register(s0);
          
      float gettexel(float2 uv)
      {
        float4 col = tex2D(s0,uv);
        float g = (col.x+col.y+col.z)*(1.0f/3.0f);
        g = (g+maskpar.x)*maskpar.y;  
        g = saturate(g);
        return g;
      }

      void main
      (
        in float2 uv : TEXCOORD0,        
        out float4 result : COLOR0
      )
      {      
        float l; 
        pif(EDGE)
        { 
          float g1 = gettexel(uv+float2(0,0));
          float g2 = gettexel(uv+float2(textpar.x,0));
          float g3 = gettexel(uv+float2(0,textpar.y));

          g2 = g1-g2;
          g3 = g1-g3;

          l = length(float2(g2,g3));
        }
        pelse
        {
          l = gettexel(uv+float2(0,0));
        }
        result = float4(l,l,l,1.0f);
      }
    }
  }

  header
  {
    enum ExtraBits
    {
      EXTRA_EDGE  = 1<<1,
    };
    sInt Extra;
  }
  new
  {
    Extra = 0;
  }

  prepare
  {
    sInt ps=0;
    
    if (Extra&EXTRA_EDGE)
    {
      ps |= tGlowMaskMatPSPermMask_EDGE;
    }

    VertexShader = VS();    
    PixelShader = PS(ps);        
  }
};


material tGlowCombMat
{
  vs
  {
    asc vs_3_0                 // hlsl code
    {      
      cbuffer tGlowCombMatVSPara : register(c0) : slot vs 0
      { 
        row_major float4x4 mvp;     
      };

      use tGlowCombMatVSPara;
      
      void main
      (
        in float3 in_pos : POSITION,
        in float4 in_color : COLOR0,
        in float2 in_uv0 : TEXCOORD0,
        out float2 out_uv0 : TEXCOORD0,
        out float4 out_pos : POSITION,
      )
      {
        out_uv0 = in_uv0;
        out_pos = mul(float4(in_pos,1),mvp);
      }      
    }
  }

  ps
  {
    asc ps_3_0
    {
      cbuffer tGlowCombMatPSPara : register(c0) : slot ps 0
      { 
        float4 textpar; //.x= 1/width, .y= 1/height
      };

      permute tGlowCombMatPSPerm
      {
        ADD;      
        BLEND;
        MULTIPLY;
        SUB;
      };

      use tGlowCombMatPSPara;
      use tGlowCombMatPSPerm;

      sampler2D s0 : register(s0);
      sampler2D s1 : register(s1);  //blur
      sampler2D s2 : register(s2);  //gradient

      void main
      (
        in float2 uv : TEXCOORD0,        
        out float4 result : COLOR0
      )
      {      
        float4 c1 = tex2D(s0,uv);
        float4 t = tex2D(s1,uv);
        float4 c2 = tex2D(s2,float2(t.x,0));
        float r1 = c1.x;
        float g1 = c1.y;
        float b1 = c1.z;
        float r2 = c2.x;
        float g2 = c2.y;
        float b2 = c2.z;

        pif(BLEND)
        {
          r1 = 1.0 - (1.0 - r1) * (1.0 - r2);
          g1 = 1.0 - (1.0 - g1) * (1.0 - g2);
          b1 = 1.0 - (1.0 - b1) * (1.0 - b2);
        }
        pif(ADD)
        {
          r1 = r1 + r2;
          g1 = g1 + g2;
          b1 = b1 + b2;
        }
        pif(SUB)
        {
          r1 = r1 - r2;
          g1 = g1 - g2;
          b1 = b1 - b2;
        }
        pif(MULTIPLY)
        {
          r1 = r1 * r2;
          g1 = g1 * g2;
          b1 = b1 * b2;
        }
        result = float4(r1,g1,b1,1.0);
      }
    }
  }

  header
  {
    enum ExtraBits
    {
      EXTRA_ADD      = 1<<1,
      EXTRA_BLEND    = 1<<2,
      EXTRA_MULTIPLY = 1<<3,
      EXTRA_SUB      = 1<<4
    };
    sInt Extra;
  }
  new
  {
    Extra = 0;
  }

  prepare
  {
    sInt ps=0;
    
    if (Extra&EXTRA_ADD)
    {
      ps |= tGlowCombMatPSPermMask_ADD;
    }
    if (Extra&EXTRA_BLEND)
    {
      ps |= tGlowCombMatPSPermMask_BLEND;
    }
    if (Extra&EXTRA_MULTIPLY)
    {
      ps |= tGlowCombMatPSPermMask_MULTIPLY;
    }
    if (Extra&EXTRA_SUB)
    {
      ps |= tGlowCombMatPSPermMask_SUB;
    }
    VertexShader = VS();    
    PixelShader = PS(ps);        
  }
};









material tFocusBlurMat
{
  vs
  {
    asc vs_3_0                 // hlsl code
    {      
      cbuffer tFocusBlurMatVSPara : register(c0) : slot vs 0
      { 
        row_major float4x4 mvp;     
      };

      use tFocusBlurMatVSPara;
      
      void main
      (
        in float3 in_pos : POSITION,        
        in float2 in_uv0 : TEXCOORD0,
        out float2 out_uv0 : TEXCOORD0,
        out float4 out_pos : POSITION,
      )
      {
        out_uv0 = in_uv0;
        out_pos = mul(float4(in_pos,1),mvp);
      }      
    }
  }

  ps
  {
    asc ps_3_0
    {
      cbuffer tFocusBlurMatPSPara : register(c0) : slot ps 0
      { 
        float4 focuspoint;
      };
      
      use tFocusBlurMatPSPara;

      sampler2D s0 : register(s0);  
      sampler2D s1 : register(s1);  
          
      void main
      (
        in float2 in_uv : TEXCOORD0,        
        out float4 result : COLOR0
      )
      {              
        float4 bt=tex2D(s0,in_uv);
        float4 st=tex2D(s1,in_uv);        
        float2 f=(in_uv.xy-focuspoint.xy)*2.0f;                
        float bf=length(f);
        bf=bf*focuspoint.z+focuspoint.w;
        bf=clamp(bf,0.0,1.0);
        result = lerp(st,bt,bf);        
      }
    }
  }
};


/*
material tWipeMat
{
  vs
  {
    asc vs_3_0                 // hlsl code
    {      
      cbuffer tWipeMatVSPara : register(c0) : slot vs 0
      { 
        row_major float4x4 mvp;     
      };

      use tWipeMatVSPara;
      
      void main
      (
        in float3 in_pos : POSITION,
        in float2 in_uv0 : TEXCOORD0,
        out float4 out_pos : POSITION,
        out float2 out_uv0 : TEXCOORD0
      )
      {
        out_uv0 = in_uv0;
        out_pos = mul(float4(in_pos,1),mvp);
      }      
    }
  }

  ps
  {
    asc ps_3_0
    {
      cbuffer tWipeMatPSPara : register(c0) : slot ps 0
      { 
        float4 para;
      };


      use tWipeMatPSPara;

      sampler2D s0 : register(s0);  //image 1
      sampler2D s1 : register(s1);  //image 2
      sampler2D s2 : register(s2);  //alpha mask

      void main
      (
        in float2 uv : TEXCOORD0,        
        out float4 result : COLOR0
      )
      {      
        float4 c1 = tex2D(s0,uv);
        float4 c2 = tex2D(s1,uv);
        float4 c3 = tex2D(s2,uv);

        float r1 = c1.x;
        float g1 = c1.y;
        float b1 = c1.z;

        float r2 = c2.x;
        float g2 = c2.y;
        float b2 = c2.z;

        float a = uv.x;//(clamp(c3.x,para.z,para.w)+para.x)*para.y;

        r1 = lerp(r1,r2,a);
        g1 = lerp(g1,g2,a);
        b1 = lerp(b1,b2,a);

        result = float4(r1,g1,b1,1.0);
      }
    }
  }
};
*/